package kk.yy.myaccount;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Window;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import kk.yy.myaccount.databinding.ActivityMainBinding;
import kk.yy.myaccount.main.MainFragment;
import kk.yy.myaccount.statistic.StatisticFragment;

public class MainActivity extends AppCompatActivity  implements BottomNavigationBar.OnTabSelectedListener{
    private ActivityMainBinding mainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        mainBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        setContentView(mainBinding.getRoot());
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView(){
        /** 导航基础设置 包括按钮选中效果 导航栏背景色等 */
        mainBinding.bottomNavigationBar
                .setTabSelectedListener(MainActivity.this)
                .setMode(BottomNavigationBar.MODE_FIXED)
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT)
                .setActiveColor("#CD5555") //选中颜色
                .setInActiveColor("#000000"); //未选中颜色
        mainBinding.bottomNavigationBar.setBarBackgroundColor("#FFFFFF");//导航栏背景色


        /** 添加导航按钮 */
        mainBinding.bottomNavigationBar
                .addItem(new BottomNavigationItem(R.drawable.account_icon, "账单"))
                .addItem(new BottomNavigationItem(R.drawable.mine_icon, "我的"))
                .setFirstSelectedPosition(0)
                .initialise(); //initialise 一定要放在 所有设置的最后一项
        setDefaultFragment();//设置默认页
    }

    /**
     * 设置默认导航栏
     */
    private void setDefaultFragment() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.main_content, MainFragment.getInstance());
        transaction.commit();
    }



    @Override
    public void onTabSelected(int position) {
        FragmentManager fm = getSupportFragmentManager();
        //开启事务
        FragmentTransaction transaction = fm.beginTransaction();
        switch (position) {
            case 0:
                transaction.replace(R.id.main_content, MainFragment.getInstance());
                break;
            case 1:
                transaction.replace(R.id.main_content, StatisticFragment.getInstance());
                break;
            default:
                break;
        }

        transaction.commit();// 事务提交
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }
}
